/**
 * 大乐透服务端接口类
 * @version 1.0.1
 */
class DltApi {
	/**
	 * 获取大乐透官方远程数据
	 * @returns Array 
	 * 数据结构
	 * list = [ drawing1, drawing2, ...]
	 * drawing = {lotteryDrawNum: "24023", lotteryDrawResult: "08 10 12 14 22 05 09",lotteryUnsortDrawresult: "12 14 08 10 22 05 09"}
	 */
	fetchData(isServerForce = true, isOffifial = true) {
		return new Promise((resolve, reject) => {
			let url =
				'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=60&isVerify=1&pageNo=1&termLimits=60';
			if (!isOffifial) {
				url = 'https://lottery.izishu.cn/api.php/dlt/history?refresh=' + (isServerForce ? 1 : 0);
			}
			uni.request({
				url,
				success: (res) => {
					// console.log('【fetchData 获取大乐透官方远程数据】成功', JSON.stringify(res));
					console.log('【fetchData】获取大乐透官方远程数据，成功');
					let list = [];
					if(isOffifial){
						list = (res['data'] ? res.data : res).value.list;
					}else{
						list = res['data'] ? res.data.data : res;
					}
					resolve(list.map(row => {
						const {
							lotteryDrawNum,
							lotteryDrawResult,
							lotteryDrawTime
						} = row;
						return {
							lotteryDrawNum,
							lotteryDrawResult,
							lotteryDrawTime
						};
					}));
				},
				fail: (err) => {
					console.log('【fetchData】获取大乐透官方远程数据，网络错误', err);
					reject(err);
				}
			})
		})
	}

	/**
	 * 获取订单详情
	 * @param {Number} id
	 */
	getOrderDetail(id) {
		return new Promise((resolve, reject) => {
			const url = 'https://lottery.izishu.cn/api.php/dlt/order-detail?id=' + id;
			// const url = 'http://127.0.0.1:8000/api.php/dlt/order-detail?id=' + id;
			console.log('【getOrderDetail】获取大乐透远程数据，开始', url);
			uni.request({
				url,
				success: (res) => {
					console.log('【getOrderDetail】获取大乐透远程数据，成功', JSON.stringify(res));
					// resolve((res['data'] ? res.data : res).value.list.map(row => {
					resolve(res.data);
				},
				fail: (err) => {
					console.log('【getOrderDetail】获取大乐透远程数据，网络错误', err);
					reject(err);
				}
			})
		})
	}
}

export default DltApi;